package com.zhaoxi.project.sys.controller;

import java.util.ArrayList;
import java.util.List;

import com.zhaoxi.project.sys.service.impl.SysMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.sys.domain.SysMenu;

/**
 * 菜单信息
 *
 * @author zhaoxi
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseService {
  @Autowired
  private SysMenuServiceImpl menuService;

  /**
   * 菜单管理：获取菜单列表（xieleilei 12.3）
   */
  @PreAuthorize("@ss.hasPermi('sys:menu:list')")
  @GetMapping("/list")
  public PageData list(SysMenu menu) {
    List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
    return success(menus);
  }

  /**
   * 根据菜单编号获取详细信息
   */
  @PreAuthorize("@ss.hasPermi('sys:menu:query')")
  @GetMapping(value = "/{menuId}")
  public PageData getInfo(@PathVariable Long menuId) {
    return success(menuService.selectMenuById(menuId));
  }

  /**
   * 获取菜单下拉树列表
   */
  @GetMapping("/treeselect")
  public PageData treeselect(SysMenu menu) {
    List<SysMenu> list = menuService.selectMenuList(menu,getUserId());
    List<SysMenu> auths = new ArrayList<SysMenu>();
    List<SysMenu> menus = new ArrayList<SysMenu>();

    for (SysMenu m : list){
      if (m.getMenuType().equals("P") || m.getMenuType().equals("F")){
        auths.add(m);
      }else if (m.getMenuType().equals("M") || m.getMenuType().equals("C")){
        menus.add(m);
      }
    }
    PageData ajax = PageData.success();
    ajax.put("menus", menuService.buildMenuTreeSelect(menus));
    ajax.put("auths", menuService.buildMenuTreeSelect(auths));
    return ajax;
  }

  /**
   * 加载对应角色菜单列表树
   */
  @GetMapping(value = "/roleMenuTreeselect/{roleId}")
  public PageData roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
    List<SysMenu> list = menuService.selectMenuList(getUserId());
    List<SysMenu> auths = new ArrayList<SysMenu>();
    List<SysMenu> menus = new ArrayList<SysMenu>();

    for (SysMenu m : list){
      if (m.getMenuType().equals("P") || m.getMenuType().equals("F")){
        auths.add(m);
      }else if (m.getMenuType().equals("M") || m.getMenuType().equals("C")){
        menus.add(m);
      }
    }

    PageData ajax = PageData.success();
    ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
    ajax.put("menus", menuService.buildMenuTreeSelect(menus));
    ajax.put("auths", menuService.buildMenuTreeSelect(auths));
    return ajax;
  }

  /**
   * 新增菜单
   */
  @PreAuthorize("@ss.hasPermi('sys:menu:add')")
  @Log(title = "菜单管理", businessType = BusinessType.INSERT)
  @PostMapping
  public PageData add(@Validated @RequestBody SysMenu menu) {
    return menuService.insertMenu(menu);
  }

  /**
   * 修改菜单
   */
  @PreAuthorize("@ss.hasPermi('sys:menu:edit')")
  @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
  @PutMapping
  public PageData edit(@Validated @RequestBody SysMenu menu) {
    return menuService.updateMenu(menu);
  }

  /**
   * 删除菜单
   */
  @PreAuthorize("@ss.hasPermi('sys:menu:remove')")
  @Log(title = "菜单管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{menuId}")
  public PageData remove(@PathVariable("menuId") Long menuId) {
    if (menuService.hasChildByMenuId(menuId)) {
      return warn("存在子菜单,不允许删除");
    }
    if (menuService.checkMenuExistRole(menuId)) {
      return warn("菜单已分配,不允许删除");
    }
    return toAjax(menuService.deleteMenuById(menuId));
  }
}
